﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Design;
using EnvDTE80;
using System.IO;
using ScaffoldIt.Configuration;

namespace ScaffoldIt.Commands
{
    using EnvDTE;
    using Microsoft.VisualStudio.Shell.Interop;

    class CreateScaffoldItEnvCommand : OleMenuCommand
    {
        private ScaffoldItPackage _pkg;

        public CreateScaffoldItEnvCommand(ScaffoldItPackage pkg)
            : base(Callback, new CommandID(GuidList.guidScaffoldItCmdSet, (int)PkgCmdIDList.cmdCreateScaffoldIt))
        {
            _pkg = pkg;
            this.BeforeQueryStatus += new EventHandler(CmdBeforeQueryStatus);
        }

        void CmdBeforeQueryStatus(object sender, EventArgs e)
        {
            if (!ConfigHelpers.HasConfig())
            {
                Visible = true;
            }
            else
            {
                Visible = false;
            }
        }

        static void Callback(object sender, EventArgs e)
        {
            var DTE = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));
            var folder = Path.GetDirectoryName(DTE.Solution.FileName);            
            var scaffoldItItem = DTE.GetOrCreateScaffoldItItem();

            Project scaffoldingItem = null;
            foreach (ProjectItem item in scaffoldItItem.ProjectItems)
            {
                if (item.Name == "Templates")
                {
                    scaffoldingItem = (Project)item.Object;
                    break;
                }
            }

            if (scaffoldingItem == null)
                scaffoldingItem = ((SolutionFolder)scaffoldItItem.Object).AddSolutionFolder("Templates");
            
            ProcessSampleConfig(scaffoldItItem, folder);

            ProcessSampleTemplate(folder, scaffoldingItem);

            var wnd = (sender as CreateScaffoldItEnvCommand)._pkg.ShowToolWindow();
            wnd.RefreshWindow();
        }

        private static void ProcessSampleTemplate(string folder, Project scaffoldingItem)
        {
            var scaffoldingFolder = Path.Combine(folder, "Scaffolding");
            if (!Directory.Exists(scaffoldingFolder))
                Directory.CreateDirectory(scaffoldingFolder);

            var samplefile = Path.Combine(scaffoldingFolder, "sample template.tt");

            using (StreamWriter writer = new StreamWriter(samplefile, false, System.Text.UTF8Encoding.Default))
            {
                writer.Write(SampleTemplate.Template());
            }
            var templateItem = scaffoldingItem.ProjectItems.AddFromFile(samplefile);
            templateItem.Open();
        }

        private static void ProcessSampleConfig(Project scaffoldItItem, string folder)
        {
            var cfg = ScaffoldItConfig.SampleConfig();
            var confFile = Path.Combine(folder, "scaffolding.config");
            cfg.Save(confFile);
            var confItem = scaffoldItItem.ProjectItems.AddFromFile(confFile);
            confItem.Open();
        }
    }
}
